package gqz.mvvm.view;

import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;

import gqz.mvvm.R;

/**
 * Created by Administrator on 2017/12/9.
 */

public class AnimatorActivity extends BaseActivity implements View.OnClickListener {

    Button btn_scale, btn_translate, btn_rotate, btn_alhpa, btn_color;
    Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
    ImageView iv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        initView();
    }

    private void initView() {
        iv = (ImageView) findViewById(R.id.animator_iv);

        btn_scale = (Button) findViewById(R.id.animator_btn_scale);
        btn_translate = (Button) findViewById(R.id.animator_btn_translate);
        btn_rotate = (Button) findViewById(R.id.animator_btn_rotate);
        btn_alhpa = (Button) findViewById(R.id.animator_btn_alpha);
        btn_color = (Button) findViewById(R.id.animator_btn_color);
        btn_scale.setOnClickListener(this);
        btn_translate.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_alhpa.setOnClickListener(this);
        btn_color.setOnClickListener(this);

        btn1 = (Button) findViewById(R.id.animator_btn_accelerate);
        btn2 = (Button) findViewById(R.id.animator_btn_accelerateDecelerate);
        btn3 = (Button) findViewById(R.id.animator_btn_anticipate);
        btn4 = (Button) findViewById(R.id.animator_btn_anticipateOvershoot);
        btn5 = (Button) findViewById(R.id.animator_btn_bounce);
        btn6 = (Button) findViewById(R.id.animator_btn_cycle);
        btn7 = (Button) findViewById(R.id.animator_btn_decelerate);
        btn8 = (Button) findViewById(R.id.animator_btn_linear);
        btn9 = (Button) findViewById(R.id.animator_btn_overshoot);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
    }

    AnimatorSet set;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.animator_btn_scale:
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, View.SCALE_X, 1, 0, 1.5f, 1);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, View.SCALE_Y, 1, 0, 1.5f, 1);
                set = null;
                set = new AnimatorSet();
                set.setDuration(2000);
                set.setTarget(iv);
                set.playTogether(scaleX, scaleY);
                set.start();
                break;
            case R.id.animator_btn_translate:
                ObjectAnimator tX = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 200, 0);
                ObjectAnimator tY = ObjectAnimator.ofFloat(iv, View.TRANSLATION_Y, 0, 200, 0);
                set = null;
                set = new AnimatorSet();
                set.setDuration(2000);
                set.setTarget(iv);
                set.playSequentially(tX, tY);
                set.start();
                break;
            case R.id.animator_btn_rotate:
                ObjectAnimator rotate = ObjectAnimator.ofFloat(iv, View.ROTATION, 0, 360, 0, -360, 0);
                rotate.setDuration(2000);
                rotate.start();
                break;
            case R.id.animator_btn_alpha:
                ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, View.ALPHA, 0, 1);
                alpha.setDuration(2000);
                alpha.start();
                break;
            case R.id.animator_btn_color:
                ObjectAnimator color = ObjectAnimator.ofInt(iv, "backgroundColor", 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00, 0xffff00ff, 0xff00ffff);
                color.setEvaluator(new ArgbEvaluator());//起到一个颜色变化平缓过度效果
                color.setDuration(6000);
                color.start();
                break;
            /**
             * 常用的几种插值器：
             * AccelerateInterpolator 开始慢
             * AccelerateDecelerateInterpolator 开始和结束慢
             * AnticipateInterpolator 开始时向后
             * AnticipateOvershootInterpolator 开始时向后退一点，结束时超出一点然后回弹
             * BounceInterpolator 动画结束的时候弹起
             * CycleInterpolator 循环播放指定的次数，速率改变沿着正弦曲线
             * DecelerateInterpolator 开始快，结束慢，创建的时候，可以传factor值，如DecelerateInterpolator(2f)，值越大结束时越慢
             * LinearInterpolator 匀速
             * OvershootInterpolator 结束时超出一点然后回弹，创建的时候，可以传tension值，OvershootInterpolator(0.8f)，值越大超出越多
             */
            case R.id.animator_btn_accelerate:
                ObjectAnimator t1 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t1.setDuration(3000);
                t1.setInterpolator(new AccelerateInterpolator());
                t1.start();
                break;
            case R.id.animator_btn_accelerateDecelerate:
                ObjectAnimator t2 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t2.setDuration(3000);
                t2.setInterpolator(new AccelerateDecelerateInterpolator());
                t2.start();
                break;
            case R.id.animator_btn_anticipate:
                ObjectAnimator t3 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t3.setDuration(3000);
                t3.setInterpolator(new AnticipateInterpolator());
                t3.start();
                break;
            case R.id.animator_btn_anticipateOvershoot:
                ObjectAnimator t4 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t4.setDuration(3000);
                t4.setInterpolator(new AnticipateOvershootInterpolator());
                t4.start();
                break;
            case R.id.animator_btn_bounce:
                ObjectAnimator t5 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t5.setDuration(3000);
                t5.setInterpolator(new BounceInterpolator());
                t5.start();
                break;
            case R.id.animator_btn_cycle:
                ObjectAnimator t6 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t6.setDuration(3000);
                t6.setInterpolator(new CycleInterpolator(2));
                t6.start();
                break;
            case R.id.animator_btn_decelerate:
                ObjectAnimator t7 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t7.setDuration(3000);
                t7.setInterpolator(new DecelerateInterpolator());
                t7.start();
                break;
            case R.id.animator_btn_linear:
                ObjectAnimator t8 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t8.setDuration(3000);
                t8.setInterpolator(new LinearInterpolator());
                t8.start();
                break;
            case R.id.animator_btn_overshoot:
                ObjectAnimator t9 = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, 0, 2000);
                t9.setDuration(3000);
                t9.setInterpolator(new OvershootInterpolator());
                t9.start();
                break;
        }
    }
}
